//
//  XKWelcomeViewController.swift
//  XKWeibo
//
//  Created by 小k on 15/11/11.
//  Copyright © 2015年 小k. All rights reserved.
//

import UIKit
import SDWebImage

class XKWelcomeViewController: UIViewController {
    
    
    // MARK: - 搭建界面
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        iconHeadView.sd_setImageWithURL(XKUserAccountViewModel.sharedUserAccount.headUrl)
    }
    
    override func loadView() {
        super.loadView()
        
        // 替换view
        view = backImageView
        
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 提前更新约束
        iconHeadView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-view.bounds.height + 200)
        }
        

        addAnimation()
    }
    
    
    // MARK: - 设置动画
    private func addAnimation () {
        
        welcomeLable.alpha = 0
        
        UIView.animateWithDuration(2.0,
            delay: 0,
            usingSpringWithDamping: 0.8,
            initialSpringVelocity: 8,
            options: [],
            animations: { () -> Void in
                
                self.view.layoutIfNeeded()
                
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    }, completion: { (_) -> Void in
                        print("OK")
                        
                        // 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewController, object: nil)
                })
        }
    }
    
    // MARK: - 懒加载属性
    /// 背景图片
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 默认头像
    private lazy var iconHeadView:UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        return iconView
    }()
    
    /// 欢迎标签
    private lazy var welcomeLable:UILabel = UILabel(title: "欢迎归来", titleFont: 16)
}

// MARK: - 设置界面
extension XKWelcomeViewController {
    private func setupUI () {
        
        ///1. 添加控件
        view.addSubview(iconHeadView)
        view.addSubview(welcomeLable)
        
        /// 2. 设置布局
        iconHeadView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconHeadView.snp_centerX)
            make.top.equalTo(iconHeadView.snp_bottom).offset(16)
        }
        
        
        
    }
}
